package com.starlight.ts_clouddev.controller.authentication;

import com.starlight.ts_clouddev.annotition.AuthenticationCheck;
import com.starlight.ts_clouddev.annotition.RoleType;
import com.starlight.ts_clouddev.command.CloudUserLoginCommand;
import com.starlight.ts_clouddev.infrastructure.entity.CloudUser;
import com.starlight.ts_clouddev.service.CloudUserService;
import com.starlight.ts_clouddev.util.CloudResponse;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author yfy2001
 * @date 2023/6/4 20:58
 */
@RestController
@RequestMapping("/auth")
public class AuthenticationController {
    @Resource
    private CloudUserService cloudUserService;

    @PostMapping ("/login")
    public CloudResponse<String> login(CloudUserLoginCommand command){
        System.out.println(command.toString());
        CloudUser cloudUser=new CloudUser();
        cloudUser.setEmail(command.getEmail());
        cloudUser.setPassword(command.getPassword());
        return CloudResponse.success(cloudUserService.userLogin(cloudUser));
    }

    @PostMapping("/logout")
    @AuthenticationCheck(roleType = {RoleType.LOGIN})
    public CloudResponse<String> flowerUserLogout(HttpServletRequest request){
        String token=request.getHeader("token");
        return CloudResponse.success(cloudUserService.logout(token));
    }

    @PostMapping("/verify")
    @AuthenticationCheck(roleType = {RoleType.LOGIN})
    public CloudResponse<Boolean> verify(HttpServletRequest request){
        return CloudResponse.success(true);
    }


}
